﻿using CompanyManageSystem.DLL;
using CompanyManageSystem.Model;
using CompanyManageSystem.Service;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace CompanyManageSystem.UI.Repast
{
    public partial class Frm_PurchaseStorageOrder : BaseXtraForm
    {
        public Frm_PurchaseStorageOrder()
        {
            InitializeComponent();
            this.BindCheckEdit(gridView1, false, true);
            this.BindRowIndicator(gridView1);
        }

        /// <summary>
        /// 窗体加载完成
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Frm_PurchaseStorageOrder_Load(object sender, EventArgs e)
        {
            RepastService rservice = new RepastService();
            repositoryItemSearchLookUpEdit1.DataSource = rservice.GetAllSuppliers();
            SupplierTree.Properties.DataSource = rservice.GetAllSuppliers();
        }

        /// <summary>
        /// 增加
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void barButtonItem1_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            Frm_PurchaseStorageOrderDetailAdd frm = new Frm_PurchaseStorageOrderDetailAdd(status: EStatus.Add);
            frm.SaveEvent += StorageDetail_AddSaveEvent;
            frm.ShowDialog();
        }
        private void StorageDetail_AddSaveEvent(Object sender, EventArgs e)
        {
            PageTurning();
            this.pager1.PageRefresh();
            ShowInfo("保存成功!");
        }

        /// <summary>
        /// 编辑
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void barButtonItem2_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            var focusHeader = gridView1.GetFocusedRow() as PurchaseStorageOrder;

            if (focusHeader == null)
            {
                ShowInfo("当前没有主单，请添加!");
                return;
            }

            if (focusHeader.Examine == "已审批")
            {
                ShowInfo("已审批的主单无法修改!");
                return;
            }

            Frm_PurchaseStorageOrderDetailAdd frm = new Frm_PurchaseStorageOrderDetailAdd(status: EStatus.Modify, headerId: focusHeader.ID);
            frm.SaveEvent += StorageDetail_ModifySaveEvent;
            frm.ShowDialog();

        }
        public void StorageDetail_ModifySaveEvent(Object sender, EventArgs e)
        {
            //PageTurning();
            var header = sender as PurchaseStorageOrder;

            if (header != null)
            {
                gridView1.SetFocusedRowCellValue("OrderNum", header.OrderNum);
                gridView1.SetFocusedRowCellValue("SupplierID", header.SupplierID);
                gridView1.SetFocusedRowCellValue("StorageTime", header.StorageTime);
                gridView1.SetFocusedRowCellValue("Style", header.Style == "0" ? "现金" : "月结");
                gridView1.SetFocusedRowCellValue("Remark", header.Remark);
            }
            else
            {
                PageTurning();
            }
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void barButtonItem3_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            var rservice = new RepastService();
            var header = this.GetChecked<PurchaseStorageOrder>(gridView1);

            if (header.Count == 0)
            {
                ShowInfo("请选择需要删除的主单!");
                return;
            }

            if (ShowQuestion($"是否删除{header.Count}条主单?"))
            {
                var headerId = header.Select(p => p.ID).ToArray();
                var count = rservice.DelPurchaseOrderByID(headerId);
                ShowInfo($"成功删除条{count}主单");
                PageTurning();
            }
        }

        /// <summary>
        /// 导出Excel
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void barButtonItem11_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            var header = this.GetChecked<PurchaseStorageOrder>(gridView1);

            if (header.Count == 0)
            {
                ShowInfo("请勾选主单!");
                return;
            }

            Frm_PurchaseStorageDetail frm = new Frm_PurchaseStorageDetail();
            frm.SetGridControlValue(header, true);
        }

        /// <summary>
        /// 上一页
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void barButtonItem7_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            this.pager1.PageIndex -= 1;
            PageTurning();
        }

        /// <summary>
        /// 下一页
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void barButtonItem8_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            this.pager1.PageIndex += 1;
            PageTurning();
        }

        #region 分页控件
        /// <summary>
        /// 分页控件加载完成
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void pageControl1_Load(object sender, EventArgs e)
        {
            var rservice = new RepastService();
            var count = rservice.GetStorageOrderByFactor(DateTime.MinValue, DateTime.MinValue, DateTime.MinValue, DateTime.MinValue, null, null, null).Count();

            this.pager1.PageSize = 20;
            this.pager1.PageIndex = 1;
            this.pager1.TotalCount = count;
            this.PageTurning();
        }

        /// <summary>
        /// 分页数据刷新
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void pageControl1_DataRefresh(object sender, EventArgs e)
        {
            this.PageTurning();
        }

        //分页方法
        private void PageTurning()
        {
            var date1 = dateEdit1.DateTime;
            var date2 = dateEdit2.DateTime;
            var date3 = dateEdit3.DateTime;
            var date4 = dateEdit4.DateTime;
            Guid? supplierId = null;
            if (!string.IsNullOrEmpty(SupplierTree.EditValue.ToString()))
            {
                supplierId = (Guid)SupplierTree.EditValue;
            }
            var style = "";
            if (!string.IsNullOrEmpty(Style.Text))
            {
                style = Style.Text;
            }
            var orderNum = OrderNum.Text;

            RepastService rservice = new RepastService();
            var skipNum = (this.pager1.PageIndex - 1) * this.pager1.PageSize;
            var listData = rservice.GetStorageOrderByFactor(date1, date2, date3, date4, supplierId, style, orderNum);
            //获取跳转后信息
            gridControl1.DataSource = listData.Skip(skipNum).Take(this.pager1.PageSize);

            //获取不跳转时的总数
            var count = listData.Count;
            if (count % this.pager1.PageSize == 0)
            {
                //页总数
                this.pager1.PageCount = count / this.pager1.PageSize;
            }
            else
            {
                this.pager1.PageCount = (count / this.pager1.PageSize) + 1;
            }
            //信息总数
            this.pager1.TotalCount = count;

            #region 上一页/下一页按钮是否允许编辑
            this.barButtonItem7.Enabled = true;
            this.barButtonItem8.Enabled = true;
            if (this.pager1.PageIndex == 1)
            {
                this.barButtonItem7.Enabled = false;
            }
            if (this.pager1.PageIndex == this.pager1.PageCount)
            {
                this.barButtonItem8.Enabled = false;
            }
            #endregion
        }
        #endregion

        #region
        /// <summary>
        /// 重绘单元格样式
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void gridView1_CustomDrawCell(object sender, DevExpress.XtraGrid.Views.Base.RowCellCustomDrawEventArgs e)
        {
            if (e.Column.FieldName == "Examine")
            {
                if (e.DisplayText == "未审批")
                {
                    e.Appearance.ForeColor = Color.Red;
                }
                else
                {
                    e.Appearance.ForeColor = Color.Green;
                }
            }
        }

        #region 审批/弃审
        /// <summary>
        /// 审批
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void barButtonItem4_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            ChangeStatus("1");
        }

        /// <summary>
        /// 弃审
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void barButtonItem5_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            ChangeStatus("0");
        }

        /// <summary>
        /// 审批方法(0:弃审 1:审批)
        /// </summary>
        private void ChangeStatus(string num)
        {
            var header = this.GetChecked<PurchaseStorageOrder>(gridView1);

            if (header.Count == 0)
            {
                var msg = num == "0" ? "请勾选需要弃审的主单!" : "请勾选需要审批的主单!";
                ShowInfo(msg);
            }

            RepastService rservice = new RepastService();
            rservice.ChangeExamineStatus(num, header);

            foreach (var item in header)
            {
                item.Examine = num == "0" ? "未审批" : "已审批";
            }
            gridControl1.RefreshDataSource();
        }
        #endregion

        /// <summary>
        /// 出入明细
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void barButtonItem12_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            var header = this.GetChecked<PurchaseStorageOrder>(gridView1);

            if (header.Count == 0)
            {
                ShowInfo("请勾选主单!");
                return;
            }

            Frm_PurchaseStorageDetail frm = new Frm_PurchaseStorageDetail();
            frm.SetGridControlValue(header, false);
            frm.ShowDialog();
        }

        /// <summary>
        /// 合并单元格事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void gridView1_CellMerge(object sender, DevExpress.XtraGrid.Views.Grid.CellMergeEventArgs e)
        {
            if (e.Column.FieldName == "CreateTime")
            {
                DateTime val1 = (DateTime)gridView1.GetRowCellValue(e.RowHandle1, e.Column);
                DateTime val2 = (DateTime)gridView1.GetRowCellValue(e.RowHandle2, e.Column);
                e.Merge = val1.Date == val2.Date;
                //已经操作过，将不再处理
                e.Handled = true;
            }

            if (e.Column.FieldName == "StorageTime")
            {
                DateTime val1 = (DateTime)gridView1.GetRowCellValue(e.RowHandle1, e.Column);
                DateTime val2 = (DateTime)gridView1.GetRowCellValue(e.RowHandle2, e.Column);
                e.Merge = val1.Date == val2.Date;
                e.Handled = true;
            }
        }

        /// <summary>
        /// 双击事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void gridView1_DoubleClick(object sender, EventArgs e)
        {
            var focusHeader = gridView1.GetFocusedRow() as PurchaseStorageOrder;
            Frm_PurchaseStorageOrderDetailAdd frm = new Frm_PurchaseStorageOrderDetailAdd(status: EStatus.Look, headerId: focusHeader.ID);
            frm.ShowDialog();
        }

        /// <summary>
        /// 查找
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Btn_Find_Click(object sender, EventArgs e)
        {
            this.pager1.PageIndex = 1;
            this.pager1.PageRefresh();
        }
        #endregion

    }
}
